//
//  ViewController.m
//  LPFTableViewSection
//
//  Created by 李鹏飞 on 2018/3/15.
//  Copyright © 2018年 AFlyLi. All rights reserved.
//

#import "ViewController.h"
#import "SectionModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}
//override get method
- (NSMutableArray *)modelArr{
    _modelArr = [[NSMutableArray alloc] init];
    for (int i = 1; i < 20; i++) {
        SectionModel *model = [[SectionModel alloc] init];
        
        model.title = [NSString stringWithFormat:@"第%d组", i];
       
        [_modelArr addObject:model];
    }
    return _modelArr;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //获取分区model
    SectionModel *model = self.modelArr[section];
    
    if (model.sectionSwitch) {
        //展开
       return model.arrOfData.count;
    }else {
        //闭合
        return 0;
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.modelArr.count;
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    SectionModel *model = self.modelArr[indexPath.section];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",model.arrOfData[indexPath.row]];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    headerView.backgroundColor = UIColor.brownColor;
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
//设置footer高度 防止闭合时header连接在一起难以区分
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
